package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Solution;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.example.springboot.service.ICreativeService;
import com.example.springboot.entity.Creative;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/creative")
    public class CreativeController {

    @Resource
    private ICreativeService creativeService;

    //更新
    @PostMapping
    public Result save(@RequestBody Creative creative) {
        return creativeService.updateCreative(creative);
    }

    //更新自己的创意
    @PostMapping("/saveMine")
    public Result saveMine(@RequestBody Creative creative) {
        creative.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return creativeService.updateCreative(creative);
    }

    //插入创意
    @PostMapping("/insertCreative")
    public Result insertCreative(@RequestBody Creative creative) {
        creative.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return Result.success(creativeService.insertCreative(creative));
    }

    //用户管理创意仓库
    @PostMapping("/manageCreative")
    public Result manageCreative(@RequestBody Creative creative) {
        creative.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return creativeService.manageCreative(creative);
    }

    //用户从回收站恢复创意
    @PostMapping("/recoveryCreative")
    public Result recoveryCreative(@RequestBody Creative creative) {
        creative.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return creativeService.recoveryCreative(creative);
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result = creativeService.removeCreativeInfo(id);
        if (result)
            return Result.success(true);
        else
            return Result.error(Constants.CODE_600,"创意不存在");
    }

    //根据id批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(creativeService.removeByIds(ids));
    }

    //社区 获取创意信息及评论数和收藏数，有待优化
    @GetMapping
    public Result findAll() {
        return Result.success(creativeService.findAll());
    }

    //通过id查找创意
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        if (id == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        Creative creative;
        try {
            creative = creativeService.getById(id);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (creative == null)
            throw new ServiceException(Constants.CODE_600,"未找到创意");
        return Result.success(creative);
    }

    //通过用户id查找创意
    @GetMapping("/getByUser")
    public Result getByUser(@RequestParam Integer userId) {
        return Result.success(creativeService.getByUser(userId));
    }

    //个人中心创意管理
    @GetMapping("/getMine")
    public Result getMine() {
        return Result.success(creativeService.getCenter(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId()));
    }

    //社区中、通过输入关键词，对创意名称和用户昵称进行匹配查找
    @GetMapping("/getBySearch")
    private Result getBySearch(@RequestParam String keyWord){
        return Result.success(creativeService.getBySearch(keyWord));
    }

    //社区中、获取发布过创意的用户数
    @GetMapping("/getUserCount")
    public Result getUserCount() {
        return Result.success(creativeService.getUserCount());
    }

    //通过id获取创意及用户信息
    @GetMapping("/getDetails")
    public Result getDetails(@RequestParam Integer id){
        return Result.success(creativeService.getDetails(id));
    }

    //创意访问量+1
    @PostMapping("/updateView")
    public Result updateView(@RequestBody Map<String, Integer> map) {
        return Result.success(creativeService.updateView(map.get("id")));
    }

    //管理员获取已发布创意
    @GetMapping("/adminGetPublished")
    public Result adminGetPublished(@RequestParam(defaultValue = "1") int PageNum,
                                    @RequestParam(defaultValue = "5") int PageSize,
                                    @RequestParam(defaultValue = "")  Integer userId,
                                    @RequestParam(defaultValue = "") String name,
                                    @RequestParam(defaultValue = "new") String sortWay) {
        return creativeService.adminGetPublished(PageNum,PageSize,userId,name,sortWay);
    }

    //管理员获取待审核方案创意
    @GetMapping("/adminGetToExamine")
    public Result adminGetToExamine(@RequestParam(defaultValue = "1") int PageNum,
                                    @RequestParam(defaultValue = "5") int PageSize,
                                    @RequestParam(defaultValue = "")  Integer userId,
                                    @RequestParam(defaultValue = "") String name) {
        return creativeService.adminGetToExamine(PageNum,PageSize,userId,name);
    }

    //管理员通过审核方案
    @PostMapping("/toPublish")
    public Result toPublish(@RequestBody Map<String, Integer> map) {
        return creativeService.toPublish(map.get("id"));
    }

    //管理员通过审核方案
    @PostMapping("/toUnPublish")
    public Result toUnPublish(@RequestBody Map<String, Integer> map) {
        return creativeService.toUnPublish(map.get("id"));
    }

    @GetMapping("/isMine")
    public Result isMine(@RequestParam Integer id) {
        Integer userId = Objects.requireNonNull(TokenUtils.getCurrentUser()).getId();
        QueryWrapper<Creative> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("user_id",userId);
        Creative creative;
        try {
            creative = creativeService.getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (creative == null)
            return Result.error(Constants.CODE_400,"参数错误");
        return Result.success(true);
    }
}

